﻿using System.Security.Cryptography;
using System.Text;

namespace Comum
{
    public class Seguranca
    {
        #region -- Membros e Construtores -------------------------------------------------------------------------------------------------
        private const int Key = 129;
        #endregion ------------------------------------------------------------------------------------------------------------------------

        #region -- Metodos ----------------------------------------------------------------------------------------------------------------
        public static string EncryptDecrypt(string textToEncrypt)
        {
            var inSb = new StringBuilder(textToEncrypt);
            var outSb = new StringBuilder(textToEncrypt.Length);

            for (var i = 0; i < textToEncrypt.Length; i++)
            {
                var c = inSb[i];
                c = (char)(c ^ Key);
                outSb.Append(c);
            }
            return outSb.ToString();
        }

        public static string Hash(string input)
        {
            SHA1 sha = new SHA1CryptoServiceProvider();
            var data = Encoding.ASCII.GetBytes(input);
            var hash = sha.ComputeHash(data);

            var sb = new StringBuilder();
            for (var i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
        #endregion ------------------------------------------------------------------------------------------------------------------------
    }
}
